package com.hyphenate.easeui.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.EaseUI;
import com.hyphenate.easeui.EaseUI.EaseUserProfileProvider;
import com.hyphenate.easeui.domain.EaseUser;

import javax.microedition.khronos.opengles.GL;

/**
 * 图片地址 默认是测试地址
 */
public class EaseUserUtils {

    static EaseUserProfileProvider userProvider;

    static {
        userProvider = EaseUI.getInstance().getUserProfileProvider();
    }

    /**
     * get EaseUser according username
     *
     * @param username
     * @return
     */
    public static EaseUser getUserInfo(String username) {
        Log.e("周康别人的EaseUserUtils id", username);
        if (userProvider != null) {
            return userProvider.getUser(username);
        }


        return null;
    }

    /**
     * set user avatar
     *
     * @param username
     */
    public static void setUserAvatar(Context context, String username, ImageView imageView) {
        Log.e("周康别人的setUserAvatar id", username);

        EaseUser user = getUserInfo(username);
        if (user != null) {
            if (user.getAvatar() != null) {
                try {
                    String avatar = user.getAvatar();
                    if (avatar.equals("") || avatar == null) {
                        Glide.with(context).load((R.drawable.ease_default_avatar)).into(imageView);
                        Log.e("周康", "setUserAvatar" + "http://test99.yunyikang.cn/" + avatar);
                    } else {

                        Glide.with(context).load("https://www.yunyikang.cn" + avatar).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
                    }
                } catch (Exception e) {
                    //use default avatar
                    Glide.with(context).load(R.drawable.ease_default_avatar).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);

                }
            } else {

                Glide.with(context).load(R.drawable.ease_default_avatar).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
            }
        }
//        else {
//            //  TODO  周康   这快哟问题哦。
//            Glide.with(context).load((R.drawable.ease_default_avatar)).into(imageView);
//        }
    }

    /**
     * set user's nickname
     */
    public static void setUserNick(String username, TextView textView) {
        Log.e("周康", "setUserNick");
        Log.e("周康别人的setUserNick id", username);

        if (textView != null) {

            EaseUser user = getUserInfo(username);
            if (user != null) {
                if (user.getNickname().length() < 0 || user.getNickname().equals("null")) {
                    textView.setText("云医康医生");

                } else {
                    textView.setText(user.getNickname());
                }
//                if (TextUtils.isEmpty(user.getNickname())) {
//
//                } else {
//
//                }
            }else{
                textView.setText("云医康医生");
            }
//            Log.e("周康", "setUserAvatar" + user.getNick() + "," + user.getNickname() + "," + user.getUsername() + "," + user.getInitialLetter());

        }

    }

}
